package com.cpg.school.controller.service;

import com.cpg.school.mybatis.mapper.PrincipalMapper;
import com.cpg.school.mybatis.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TeacherService {

    @Autowired
    private PrincipalMapper principalMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @PostMapping("/student_add.do")
    public String student_add(int grade_id, String student_name, String login_name, String login_pass) {
        //判断用户登录名是否存在
        int have = principalMapper.login_name_ishave(login_name);
        if (have > 0) return "登录名已存在，请重设。";
        Map param = new HashMap();
        param.put("id", 0);
        param.put("grade_id", grade_id);
        param.put("student_name", student_name);
        param.put("login_name", login_name);
        param.put("login_pass", login_pass);
        int add_user = teacherMapper.add_user_student(param);
        if (add_user == 0) return "新增登录名失败！";
        int put_user_grade = teacherMapper.put_user_grade(param);
        return put_user_grade > 0 ? "添加成功！" : "添加失败！";
    }

    @PostMapping("/student_import.do")
    public String student_import(int grade_id, @RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        FileInputStream inputStream = (FileInputStream) file.getInputStream();
        InputStreamReader read = new InputStreamReader(inputStream, "utf-8");
        BufferedReader br = new BufferedReader(read);//使用缓冲流按行读取
        String lineText = null;
        while ((lineText = br.readLine()) != null) {
            String[] arr = lineText.split("\\s+");
            student_add(grade_id, arr[0], arr[1], arr[2]);
        }
        br.close();
        read.close();
        return "上传成功";
    }

    @PostMapping("/student_edit.do")
    public String student_edit(int id, String student_name, String login_name, String login_pass) {
        Map param = new HashMap();
        param.put("id", id);
        param.put("student_name", student_name);
        param.put("login_name", login_name);
        param.put("login_pass", login_pass);
        int put_user_grade = teacherMapper.update_user_student(param);
        return put_user_grade > 0 ? "修改成功！" : "修改失败！";
    }

    @GetMapping("/student_del.do")
    public String student_del(int id) {
        int del_user = teacherMapper.student_del_user(id);
        int put_user_grade = del_user > 0 ? teacherMapper.student_del(id) : 0;
        return put_user_grade > 0 ? "删除成功！" : "删除失败！";
    }

    @PostMapping("/question_bank_add.do")
    public String question_bank_add(HttpSession session, int question_type, String question_name, String question_explain) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        int add_status = teacherMapper.question_bank_add(teacher_id, question_type, question_name, question_explain);
        return add_status > 0 ? "添加成功！" : "添加失败！";
    }

    @PostMapping("/question_bank_edit.do")
    public String question_bank_edit(HttpSession session, int id, int question_type, String question_name, String question_explain) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        int add_status = teacherMapper.question_bank_edit(id, question_type, question_name, question_explain);
        return add_status > 0 ? "修改成功！" : "修改失败！";
    }

    @GetMapping("/question_bank_del.do")
    public String question_bank_del(HttpSession session, int id) {
        int teacher_id = Integer.parseInt(session.getAttribute("user_id").toString());
        int del_question = teacherMapper.question_bank_del(id, teacher_id);
        if (del_question > 0) teacherMapper.question_bank_del_subjects(id, teacher_id);
        return del_question > 0 ? "删除成功！" : "删除失败！";
    }

    @PostMapping("/question_subjects_add.do")
    public String question_subjects_add(Model model, HttpSession session, int id, int type, String title, String choice, String right_key, int fraction) {
        int add_status = teacherMapper.question_subjects_add(id, type, title, choice, right_key, fraction);
        return add_status > 0 ? "添加成功！" : "添加失败！";
    }

    @PostMapping("/question_subjects_edit.do")
    public String question_subjects_edit(HttpSession session, int id, int type, String title, String choice, String right_key, int fraction) {
        int status = teacherMapper.question_subjects_edit(id, type, title, choice, right_key, fraction);
        return status > 0 ? "修改成功！" : "修改失败！";
    }

    @GetMapping("/question_subjects_del.do")
    public String question_subjects_del(HttpSession session, int id) {
        int del_question = teacherMapper.question_subjects_del(id);
        return del_question > 0 ? "删除成功！" : "删除失败！";
    }

    @PostMapping("/question_bank_send_add.do")
    public String question_bank_send_add(HttpSession session, int grade_id, int question_id) {
        int status = teacherMapper.question_bank_send_add(grade_id, question_id);
        return status > 0 ? "布置成功！" : "布置失败！";
    }

    @PostMapping("/question_bank_send_edit.do")
    public String question_bank_send_edit(HttpSession session, int id, int grade_id, int question_id) {
        int status = teacherMapper.question_bank_send_edit(id, grade_id, question_id);
        return status > 0 ? "修改成功！" : "修改失败！";
    }

    @GetMapping("/question_bank_send_del.do")
    public String question_bank_send_del(HttpSession session, int id) {
        int del_question = teacherMapper.question_bank_send_del(id);
        return del_question > 0 ? "删除成功！" : "删除失败！";
    }

    @PostMapping("/student_answers_list_count.do")
    public int student_answers_list_count(HttpSession session, int grade_id, int question_id) {
        int count = teacherMapper.student_answers_list_count(grade_id, question_id);
        int pageCount = 1;
        if (count > 15) {
            int ys = count % 15;
            pageCount = (count - count % 15) / 15;
            if (ys > 0) pageCount += 1;
        }
        return pageCount;
    }

    @PostMapping("/student_answers_correct.do")
    public String student_answers_correct(HttpSession session, int uid, int quid, int qid, int status) {
        int correct = 0;
        if (uid == 0) {
        } else {
            correct = teacherMapper.student_answers_correct(uid, qid, status);
            System.out.println(correct);
        }
        return correct > 0 ? "修改成功！" : "修改失败！";
    }

}
